<?php


namespace app\common\controller;


class Zhihuixin
{
    private $mchid;
    private $version;
    private $key;
   /**
    * 智慧薪账号信息
    */
   public function  __construct()
   {
       $this->mchid = '3803766782787846144';
       $this->version = '2.0';
       $this->key='34ffced59d9749008dfda8ca5232659a';
   }
    /**
     * 用户注册
     */
    public function myReg($data){

       $arr=$this->allarray();
       if (!is_array($data))return ['return_code'=>400,'return_msg'=>'参数不正确'];
       $arr['tel' ]=$data['phone'];
        $url = 'https://saasapi.zihuix.com/api/v2/open/register';//  http://zhxapiv1.52zeiduo.com
        $str    = $this->ToUrlParams($arr);
        $sign   = $this->sign($str);//签名
        $arr['key_sign'] = $sign;
        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $arr,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);
        return $ret;
    }

    /**
     * 用户上传资料
     */
    public function useruoloud($data){
        if (!is_array($data))return ['return_code'=>400,'return_msg'=>'参数不正确'];

        $mch_id = $this->mchid;
        $type   = '01';
        $value  = $data['freelance_id'];
        $path = getcwd() .$data['image'];
        $body = [
            [ 'name'=>'image',
                'contents'=>fopen( $path, 'rb'),
                'headers' => ['Content-Type'  => 'multipart/form-data; charset=utf-8',],
            ]
        ];
        $url = "https://saasapi.zihuix.com/api/v2/open/uploadfile?mch_id={$mch_id}&type={$type}&value={$value}";

        $client = new \GuzzleHttp\Client(['verify'=>false]);



        $ret = $client->request('POST',$url,['multipart' => $body]);
//        print_r($ret);die;
        $ret = json_decode($ret->getBody()->getContents(),1);
        return  $ret;
    }

    /**
     *  实名认证
     */
    public function realname($data){
        $img1 = $data['zupload'];
        $img2 = $data['fupload'];
        $time = time();
        $orderNo = date('YmdHis',$time) . round(1000,9999);
        $data = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'freelance_id'      => $data['freelance_id'],
            'name'              =>  $data['realname'],
            'mobile'            => $data['mobile'],
            'card_number'       => $data['idcardnum'],
            'card_positive_img' => $img1,
            'card_negative_img' => $img2,
            'bank_number'       => $data['bankid'],
            'bank_name'         => $data['bankname'],
        ];

        $str    = $this->ToUrlParams( $data );
        $sign   = $this->sign($str);//签名

        $url = 'https://saasapi.zihuix.com/api/v2/open/appraisalauthority';
        $data['key_sign'] = $sign;


        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $data,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);

        return $ret;
    }
    /**
     * 用户修改信息
     */
    public function freelinfo($data){
        $img1 = $data['zupload'];
        $img2 = $data['fupload'];
        $time = time();
        $orderNo = date('YmdHis',$time) . round(1000,9999);
        $data = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'freelance_id'      => $data['freelance_id'],
            'bank_number'       => $data['bankid'],
            'bank_name'         => $data['bankname'],
        ];

        $str    = $this->ToUrlParams( $data );
        $sign   = $this->sign($str);//签名

        $url = 'https://saasapi.zihuix.com/api/v2/open/updatefreelinfo';
        $data['key_sign'] = $sign;


        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $data,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);

        return $ret;
    }

    /**
     * 查询订单信息
     */
    public function countsinfo($data){
        $arr = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'order_no'      => $data['order_no'], //用户ID
        ];
        $str    = $this->ToUrlParams($arr);
        $sign   = $this->sign($str);//签名
        $url = 'https://saasapi.zihuix.com/api/v2/open/getmersettleaccountsinfo';
        $arr['key_sign'] = $sign;
        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $arr,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);
        return $ret;
    }
    /**
     * 用户信息查询
     */
    public function userinfo($data){
        $arr = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'freelance_id'      => $data['freelance_id'], //用户ID
        ];

        $str    = $this->ToUrlParams($arr);

        $sign   = $this->sign($str);//签名
        $url = 'https://saasapi.zihuix.com/api/v2/open/freelinfoquery';
        $arr['key_sign'] = $sign;

        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $arr,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);

        return $ret;
    }
    /**
     * 查询是否签约成功
     */
    public function querysuccess($data){
        //查询签约是否成功
//        $time = time();
//        $orderNo = date('YmdHis',$time) . round(1000,9999); //随机字符串
        $data = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'project_id'        => '4016223357388193792',//他们后台创建的项目，需要审核后才能用
            'freelance_id'      => $data['freelance_id'], //用户ID
        ];
        $str    = $this->ToUrlParams( $data );
        $sign   = $this->sign($str);//签名

        $url = 'https://saasapi.zihuix.com/api/v2/open/freelesignauthstatusquery';

        $data['key_sign'] = $sign;

        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $data,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);
       return $ret;
    }
    private function ToUrlParams( $data ) {
        ksort($data);
        $buff = "";
        foreach ($data as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }
    /**
     * 领取任务
     */
    public function gettask($data){
        $time = time();
        $orderNo = date('YmdHis',$time) . round(1000,9999); //随机字符串
        $data = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'project_id'        => '4016223357388193792',//他们后台创建的项目，需要审核后才能用
            'freelance_id'      => $data['freelance_id'], //用户ID
        ];
        $str    = $this->ToUrlParams( $data );
        $sign   = $this->sign($str);//签名

        $url = 'https://saasapi.zihuix.com/api/v2/open/freelgettask';
        $data['key_sign'] = $sign;
        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $data,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);
        return $ret;
    }
    /**
     * 查看任务状态
     */
    public function taskstatus($data){
        $time = time();
        $orderNo = date('YmdHis',$time) . round(1000,9999); //随机字符串
        $data = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'project_id'        => '4016223357388193792',//他们后台创建的项目，需要审核后才能用
            'freelance_id'      => $data['freelance_id'], //用户ID
        ];
        $str    = $this->ToUrlParams( $data );
        $sign   = $this->sign($str);//签名

        $url = 'https://saasapi.zihuix.com/api/v2/open/getmerprojsigningstatus';
        $data['key_sign'] = $sign;
        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $data,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);
        return $ret;
    }
    /**
     * 签约授权
     */
    public function contract($data){
//职业者签约授权
        $time = time();
        $orderNo = date('YmdHis',$time) . round(1000,9999); //随机字符串
        $data = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'project_id'        => '4016223357388193792',//他们后台创建的项目，需要审核后才能用
            'freelance_id'      => $data['freelance_id'], //用户ID
        ];
        $str    = $this->ToUrlParams( $data );
        $sign   = $this->sign($str);//签名

        $url = 'https://saasapi.zihuix.com/api/v2/open/freelesignauth';
        $data['key_sign'] = $sign;


        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $ret = $client->request('POST',$url,['json' => $data,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);
        return $ret;
    }
    /**
     * 阶段结算---确认支付
     */
    public function tage($data){
        //确认支付
        $time = time();
        $orderNo = date('YmdHis',$time) . round(1000,9999); //随机字符串
        $order = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); //客户订单号
        $arr = [
            'version'           => '2.0',
            'current_time'      => $this->nowtime(),
            'mch_id'            => $this->mchid,
            'nonce_number'      => $this->getorder(),
            'request_number'    => $this->getorder(),
            'project_id'        => '4016223357388193792',
            'channel_id'        => 17,  //测试  支付通道
            'customer_order_no' =>$this->getorder(),
        ];
        $list = [
            [
                'customer_order_no' =>$this->getorder(),
                'freelance_id'      =>$data['freelance_id'], //用户ID
                'atax_money'        => '1',
            ]
        ];
        $arr['details_list'] = json_encode($list);
        $str    = $this->ToUrlParams( $arr );
        $sign   = $this->sign($str);//签名

        $url = 'https://saasapi.zihuix.com/api/v2/open/mersettleaccountsstage';
        $arr['key_sign'] = $sign;


        $client = new \GuzzleHttp\Client(['verify'=>false]);
        $header = [
            'Content-Type'  => 'application/json; charset=utf-8',
        ];
        $arr['details_list'] = $list;

        $ret = $client->request('POST',$url,['json' => $arr,'headers'=>$header]);
        $ret = json_decode($ret->getBody()->getContents(),1);
        return $ret;
    }
    /**
     * 获取当前时间格式
     */
    private function nowtime(){
        $time=time();
        return date('YmdHis',$time);
    }
    /**
     * 智慧薪订单号
     */
    private function getorder(){
        $time=time();
        $orderNo ="ZHX". date('YmdHis',$time) . round(1000,9999);
        return $orderNo;
    }
    /**
     * 请求接口公共数组
     */
    private function allarray(){
        $arr = [
            'version'       => $this->version,
            'current_time'  => $this->nowtime(),
            'mch_id'        => $this->mchid,
            'nonce_number'  => $this->getorder(),
            'request_number'=> $this->getorder(),
        ];
        return $arr;
    }
    /**

     * 智慧薪签名算法

     */

    public function sign($data){

        $arr=$data."&key=".$this->key;

        $key=strtoupper(md5($arr));

        return $key;

    }
    #curl请求

    public function https_request($url,$data = null){

        if(function_exists('curl_init')){



            $curl = curl_init();

            curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

            curl_setopt($curl, CURLOPT_URL, $url);

            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);

            curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);

            if (!empty($data)){

                curl_setopt($curl, CURLOPT_POST, 1);

                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

            }

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

            $output = curl_exec($curl);



            curl_close($curl);

            return $output;

        }else{

            return false;

        }

    }
}